$(document).ready(function () {
    $("#confirm").click(function () {
        var name = $("#username").val();
        var email = $("#email").val();
        var password = $("#password").val();
        var confirmPassword = $("#confirmPassword").val();
        var myReg = /^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
        if ((name == null || name == "") && (password == null || password == "") && (
            email == null || email == "")) {
            alert("请输入需要修改的信息！");
        } else {
            if (email != null && email != "" && !myReg.test(email)) {
                alert("邮箱格式不对!");
                alert(email)
            } else {
                if (password != confirmPassword) {
                    alert("密码不一致！");
                } else {
                    $.ajax({
                        type: "POST",
                        url: "/ChangeInformation/getUser",
                        dataType: "json",
                        async: false,
                        data: {
                            name: name,
                            email: email
                        },
                        success: function (data) {
                            if (name == data.name) {
                                alert("用户名已存在，请重新输入！")
                            } else {
                                if (email == data.email) {
                                    alert("邮箱被占用，请重新输入！")
                                }
                            }

                        },
                        error: function () {
                            $.ajax({
                                type: "post",
                                url: "/ChangeInformation/getNameBySession",
                                dataType: "text",
                                success: function (data) {
                                    $.ajax({
                                        type: "get",
                                        url: "/ChangeInformation/getUserByName",
                                        data: {
                                            name: data
                                        },
                                        dataType: "json",
                                        success: function (json) {
                                            if (name == null || name == "") {
                                                name = json.name
                                            }
                                            if (password == null || password == "") {
                                                password = json.password;
                                            }
                                            if (email == null || email == "") {
                                                email = json.email;
                                            }
                                            $.ajax({
                                                type: "POST",
                                                url: "/ChangeInformation/updateUser",
                                                dataType: "json",
                                                data: {
                                                    id: json.id,
                                                    name: name,
                                                    password: password,
                                                    email: email
                                                },
                                                success: function () {
                                                    alert("修改成功！")
                                                    window.location.href = "login";
                                                },
                                                error: function () {
                                                    alert("修改失败！")
                                                },
                                            })


                                        }
                                    })
                                },
                                error: function (data) {
                                }
                            })
                        }
                    })
                }

            }
        }

    })

})